package com.pktechan.api.service.impl;

import com.pktechan.api.base.BaseMapper;
import com.pktechan.api.base.BaseServiceImpl;
import com.pktechan.api.config.YspConfig;
import com.pktechan.api.dao.CloudPointFlowMapper;
import com.pktechan.api.dao.CloudPointWithdrawMapper;
import com.pktechan.api.dao.CommissionWithdrawFlowMapper;
import com.pktechan.api.entity.*;
import com.pktechan.api.enumeration.CloudPointBusinessTypeEnum;
import com.pktechan.api.enumeration.CommissionWithdrawStatusEnum;
import com.pktechan.api.sendMiniMessage.BaseSendMiniMessage;
import com.pktechan.api.sendMiniMessage.SendMiniMessageStrategy;
import com.pktechan.api.sendMiniMessage.bean.FactoryReferrerCommissionApplyMessage;
import com.pktechan.api.sendMiniMessage.enumration.SendMiniMessageEnum;
import com.pktechan.api.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service("cloudPointWithdrawService")
@Transactional
public class CloudPointWithdrawServiceImpl extends BaseServiceImpl<CloudPointWithdraw> implements CloudPointWithdrawService{
    private static final Logger logger = LoggerFactory.getLogger(CloudPointWithdrawServiceImpl.class);

    @Autowired
    private CloudPointFlowService cloudPointFlowService;
    @Autowired
    private CloudPointService cloudPointService;
    @Autowired
    private CloudPointWithdrawMapper cloudPointWithdrawMapper;
    @Autowired
    private YspConfig yspConfig;
    @Autowired
    private CloudPointNetService cloudPointNetService;


    /**
     * 酒钻提现
     *
     */
    @Override   
    public void addCloudPointWithdraw(CloudPointWithdraw cloudPointWithdraw, CustBase custBase) {
        CloudPoint curPoint = cloudPointService.getByCustId(custBase.getId());

        //添加酒钻流水记录及酒钻持仓更新
        CloudPointFlow cloudPointFlow = new CloudPointFlow(cloudPointWithdraw,curPoint);
//        cloudPointFlowService.withdrawPoint(cloudPointFlow, custBase);

        cloudPointFlow.setBalanceAfter(curPoint.getAvailableBalance().subtract(cloudPointFlow.getAmount()));//提现前积分-变动积分=提现后积分
        cloudPointFlow.setBusinessType(CloudPointBusinessTypeEnum.CONSUMPTION.getCode());//提现消费
        cloudPointFlowService.addCloudPointFlowAndUpdateCloudPoint(cloudPointFlow, curPoint);

        //添加酒钻提现记录
        cloudPointWithdraw.setCustId(custBase.getId());
        cloudPointWithdraw.setCloudPointId(curPoint.getId());
        cloudPointWithdraw.setStatus(CommissionWithdrawStatusEnum.WITHDRAW_TO_BE_COMPLETED.getCode());
        BigDecimal feeRate = yspConfig.getCloudPoint().getWithdrawRate();
        BigDecimal pointNet = cloudPointNetService.getPointNet();
        BigDecimal feeVol = cloudPointWithdraw.getAppVol().multiply(feeRate);
        BigDecimal cfmVol = cloudPointWithdraw.getAppVol().subtract(feeVol);

        cloudPointWithdraw.setFeeRate(feeRate);
        cloudPointWithdraw.setFeeAmt(feeVol);
        cloudPointWithdraw.setPointNet(pointNet);
        cloudPointWithdraw.setCfmVol(cfmVol);
        cloudPointWithdraw.setCfmAmount(cfmVol.multiply(pointNet));

        cloudPointWithdrawMapper.save(cloudPointWithdraw);
    }

    @Override
    public BaseMapper<CloudPointWithdraw> getBaseMapper() {
        return null;
    }
}
